package cn.iocoder.yudao.module.member.service.newsStudyType;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.util.MyBatisUtils;
import cn.iocoder.yudao.framework.web.core.util.WebFrameworkUtils;
import cn.iocoder.yudao.module.member.controller.admin.newsStudyType.vo.NewsStudyTypeCreateReqVO;
import cn.iocoder.yudao.module.member.controller.admin.newsStudyType.vo.NewsStudyTypeExportReqVO;
import cn.iocoder.yudao.module.member.controller.admin.newsStudyType.vo.NewsStudyTypePageReqVO;
import cn.iocoder.yudao.module.member.controller.admin.newsStudyType.vo.NewsStudyTypeUpdateReqVO;
import cn.iocoder.yudao.module.member.convert.newsStudyType.NewsStudyTypeConvert;
import cn.iocoder.yudao.module.member.dal.dataobject.newsStudyInfo.NewsStudyInfoDO;
import cn.iocoder.yudao.module.member.dal.dataobject.newsStudyInfo.NewsStudyRankingDO;
import cn.iocoder.yudao.module.member.dal.dataobject.newsStudyType.NewsStudyTypeDO;
import cn.iocoder.yudao.module.member.dal.mysql.newsStudyInfo.NewsStudyInfoMapper;
import cn.iocoder.yudao.module.member.dal.mysql.newsStudyType.NewsStudyTypeMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.*;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.member.enums.ErrorCodeConstants.NEWS_STUDY_TYPE_NOT_EXISTS;

/**
 * 新闻栏目(成果类型) Service 实现类
 *
 * @author 管理员
 */
@Service
@Validated
public class NewsStudyTypeServiceImpl implements NewsStudyTypeService  {

    @Resource
    private NewsStudyTypeMapper newsStudyTypeMapper;
    @Resource
    private NewsStudyInfoMapper newsStudyInfoMapper;
    @Override
    public Long createNewsStudyType(NewsStudyTypeCreateReqVO createReqVO) {
        // 插入
        NewsStudyTypeDO newsStudyType = NewsStudyTypeConvert.INSTANCE.convert(createReqVO);
        newsStudyType.setCreator(WebFrameworkUtils.getLoginUserName());
        newsStudyType.setUpdater(WebFrameworkUtils.getLoginUserName());
        newsStudyTypeMapper.insert(newsStudyType);
        // 返回
        return newsStudyType.getId();
    }

    @Override
    public void updateNewsStudyType(NewsStudyTypeUpdateReqVO updateReqVO) {
        // 校验存在
        this.validateNewsStudyTypeExists(updateReqVO.getId());
        // 更新
        NewsStudyTypeDO updateObj = NewsStudyTypeConvert.INSTANCE.convert(updateReqVO);
        updateObj.setUpdater(WebFrameworkUtils.getLoginUserName());
        newsStudyTypeMapper.updateById(updateObj);
    }

    @Override
    public void deleteNewsStudyType(Long id) {
        // 校验存在
        this.validateNewsStudyTypeExists(id);
        // 删除
        newsStudyTypeMapper.deleteById(id);
        newsStudyInfoMapper.updateByStudyTypeId(id);
    }

    private void validateNewsStudyTypeExists(Long id) {
        if (newsStudyTypeMapper.selectById(id) == null) {
            throw exception(NEWS_STUDY_TYPE_NOT_EXISTS);
        }
    }

    @Override
    public NewsStudyTypeDO getNewsStudyType(Long id) {
        NewsStudyTypeDO newsStudyTypeDO = newsStudyTypeMapper.selectById(id);
        if(null!=newsStudyTypeDO){
            Long aLong = newsStudyInfoMapper.selectCount(new QueryWrapper<NewsStudyInfoDO>().eq("type_id", id)
                    .eq("status", 1).eq("deleted", false));
            newsStudyTypeDO.setChildNum(aLong);
        }else{
            newsStudyTypeDO.setChildNum(0L);
        }
        return newsStudyTypeDO;
    }

    @Override
    public List<NewsStudyTypeDO> getNewsStudyTypeList(Collection<Long> ids) {
        return newsStudyTypeMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<NewsStudyTypeDO> getNewsStudyTypePage(NewsStudyTypePageReqVO pageReqVO) {
        return newsStudyTypeMapper.selectPage(pageReqVO);
    }

    @Override
    public List<NewsStudyTypeDO> getNewsStudyTypeList(NewsStudyTypeExportReqVO exportReqVO) {
        return newsStudyTypeMapper.selectList(exportReqVO);
    }

    @Override
    public List<NewsStudyTypeDO> getNewsStudyTypeList(Integer type) {
        List<NewsStudyTypeDO> list = newsStudyTypeMapper.selectList(new QueryWrapper<NewsStudyTypeDO>().eq("type", type)
                .eq("status", 1).orderByDesc("weight").orderByAsc("update_time"));
        return list;
    }

    @Override
    public PageResult<NewsStudyTypeDO> getNewsStudyTypeList(NewsStudyTypePageReqVO pageReqVO) {
        // MyBatis Plus 查询
        IPage<NewsStudyTypeDO> mpPage = MyBatisUtils.buildPage(pageReqVO);
        // 转换返回
        Map<String, Object> params = new HashMap<>();
        if(StringUtils.isNotBlank(pageReqVO.getName())){
            params.put("name",pageReqVO.getName());
        }
       if(null !=pageReqVO.getType()){
           params.put("type",pageReqVO.getType());
       }
        if(null !=pageReqVO.getStatus()){
            params.put("status",pageReqVO.getStatus());
        }
        if(null !=pageReqVO.getDeleted()){
            params.put("deleted",pageReqVO.getDeleted());
        }else{
            params.put("deleted",0);
        }
        mpPage.setRecords(newsStudyTypeMapper.getNewsStudyTypeList(mpPage, params));
        return new PageResult<>(mpPage.getRecords(), mpPage.getTotal());
    }

    @Override
    public void updateNewsStudyTypeStatus(NewsStudyTypeDO newsStudyTypeDO) {
        newsStudyTypeMapper.updateNewsStudyType(newsStudyTypeDO);
    }

    @Override
    public void updateBatch(List<Long> ids) {
        newsStudyTypeMapper.updateBatch(ids);
    }

    @Override
    public void updateNewsStudyTypeSort(List<Long> ids) {
        List<NewsStudyTypeDO> list=new ArrayList<>();
        int size = ids.size();
        for (int i = 0; i <size ; i++) {
            NewsStudyTypeDO r=new NewsStudyTypeDO();
            r.setId(ids.get(i));
            r.setWeight(size-i);
            list.add(r);
        }
        newsStudyTypeMapper.updateNewsStudyTypeSort(list);
    }
}
